package com.neuedu.cockpit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.cockpit.common.Encode;
import com.neuedu.cockpit.domain.HmCarInfo;
import com.neuedu.cockpit.mapper.HmCarInfoMapper;
import com.neuedu.cockpit.service.IHmCarInfoService;
import com.neuedu.common.core.domain.entity.SysUser;
import com.neuedu.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static com.neuedu.common.utils.SecurityUtils.getLoginUser;

/**
 * 车辆管理信息Service业务层处理
 *
 * @author neuedu
 * @date 2024-04-22
 */
@Service
public class HmCarInfoServiceImpl extends ServiceImpl<HmCarInfoMapper, HmCarInfo> implements IHmCarInfoService {
    @Autowired
    private HmCarInfoMapper hmCarInfoMapper;

    /**
     * 查询车辆管理信息
     *
     * @param id 车辆管理信息主键
     * @return 车辆管理信息
     */
    @Override
    public HmCarInfo selectHmCarInfoById(Long id) {
        return hmCarInfoMapper.selectHmCarInfoById(id);
    }

    @Override
    public HmCarInfo selectHmCarInfoByUserIdAndCarVin(Long userId, String carVin) {
        HmCarInfo hmCarInfo = new HmCarInfo();
        hmCarInfo.setCarVin(carVin);
        hmCarInfo.setUserId(userId);
        List<HmCarInfo> list = hmCarInfoMapper.selectHmCarInfoList(hmCarInfo);
        if (list == null || list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }

    @Override
    public HmCarInfo selectInfoByUserId(Long userId) {
        return hmCarInfoMapper.selectInfoByUserId(userId);
    }

    /**
     * 查询车辆管理信息列表
     *
     * @param hmCarInfo 车辆管理信息
     * @return 车辆管理信息
     */
    @Override
    public List<HmCarInfo> selectHmCarInfoList(HmCarInfo hmCarInfo) {
        hmCarInfo.setCarName(Encode.encodeStr(hmCarInfo.getCarName()));
        hmCarInfo.setCarVin(Encode.encodeStr(hmCarInfo.getCarVin()));
        hmCarInfo.setBrand(Encode.encodeStr(hmCarInfo.getBrand()));
        return hmCarInfoMapper.selectHmCarInfoList(hmCarInfo);
    }

    @Override
    public List<HmCarInfo> selectHmCarInfoListByVin(HmCarInfo hmCarInfo) {
        return hmCarInfoMapper.selectHmCarInfoListByVin(hmCarInfo);
    }

    /**
     * 新增车辆管理信息
     *
     * @param hmCarInfo 车辆管理信息
     * @return 结果
     */
    @Override
    public int insertHmCarInfo(HmCarInfo hmCarInfo) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        hmCarInfo.setCreateBy(user.getUserName());
        hmCarInfo.setCreateTime(new Date());
        return hmCarInfoMapper.insertHmCarInfo(hmCarInfo);
    }

    /**
     * 修改车辆管理信息
     *
     * @param hmCarInfo 车辆管理信息
     * @return 结果
     */
    @Override
    public int updateHmCarInfo(HmCarInfo hmCarInfo) {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        hmCarInfo.setUpdateBy(user.getUserName());
        hmCarInfo.setUpdateTime(new Date());
        return hmCarInfoMapper.updateHmCarInfo(hmCarInfo);
    }

    @Override
    public int bingCarVin(HmCarInfo hmCarInfo) {
        SysUser user = getLoginUser().getUser();
        hmCarInfo.setUserId(user.getUserId());
        hmCarInfo.setUpdateBy(user.getUserName());
        hmCarInfo.setUpdateTime(new Date());
        return hmCarInfoMapper.updateHmCarVinAndUser(hmCarInfo);
    }

    /**
     * 批量删除车辆管理信息
     *
     * @param ids 需要删除的车辆管理信息主键
     * @return 结果
     */
    @Override
    public int deleteHmCarInfoByIds(Long[] ids) {
        return hmCarInfoMapper.deleteHmCarInfoByIds(ids);
    }

    /**
     * 删除车辆管理信息信息
     *
     * @param id 车辆管理信息主键
     * @return 结果
     */
    @Override
    public int deleteHmCarInfoById(Long id) {
        return hmCarInfoMapper.deleteHmCarInfoById(id);
    }

    /**
     * 校验车架号是否已经存在
     *
     * @param vinCode 车辆车架号
     * @return 结果
     */
    public boolean checkVinOnlyOne(String vinCode) {
        HmCarInfo hmCarInfo = new HmCarInfo();
        hmCarInfo.setCarVin(vinCode);
        List<HmCarInfo> lt = hmCarInfoMapper.selectHmCarInfoList(hmCarInfo);
        if (CollectionUtils.isEmpty(lt)) {
            return false;
        } else {
            return true;
        }
    }

    public boolean checkNfcOnlyOne(String nfc) {
        HmCarInfo hmCarInfo = new HmCarInfo();
        hmCarInfo.setNfc(nfc);
        List<HmCarInfo> lt = hmCarInfoMapper.selectHmCarInfoList(hmCarInfo);
        if (CollectionUtils.isEmpty(lt)) {
            return false;
        } else {
            return true;
        }
    }

    @Transactional
    @Override
    public int resetCarVin(Long id, String vinCode) {
        // 清楚已有的车辆userid
        hmCarInfoMapper.clearCarUserId(id);

        HmCarInfo hmCarInfo = new HmCarInfo();
        SysUser user = getLoginUser().getUser();
        hmCarInfo.setCarVin(vinCode);
        hmCarInfo.setUserId(user.getUserId());
        hmCarInfo.setUpdateBy(user.getUserName());
        hmCarInfo.setUpdateTime(new Date());
        return hmCarInfoMapper.updateHmCarVinAndUser(hmCarInfo);
    }

    public int unlockUserAndCar(Long id) {
        return hmCarInfoMapper.clearCarUserId(id);
    }

    public int lockUserAndCar(HmCarInfo hmCarInfo) {
        hmCarInfo.setUpdateTime(new Date());
        hmCarInfo.setUpdateBy(getLoginUser().getUsername());
        return hmCarInfoMapper.updateHmCarInfo(hmCarInfo);
    }
}
